package com.novax.ex.activity.provider.controller;

import com.novax.ex.activity.infrastructure.entity.OperationConfigExpand;
import com.novax.ex.activity.open.api.OperationConfigApi;
import com.novax.ex.activity.open.model.query.OperationConfigOuterQuery;
import com.novax.ex.activity.open.model.response.OperationConfigExpandResponse;
import com.novax.ex.activity.open.model.response.OperationConfigOuterResponse;
import com.novax.ex.activity.provider.service.OperationConfigService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collections;
import java.util.Objects;

/**
 * @Description 活动落地页配置
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationConfigController extends BaseActivityController implements OperationConfigApi {

    @Resource
    private OperationConfigService operationConfigService;

    @Override
    public ReturnResult<PageUtil<OperationConfigOuterResponse>> page(String language, OperationConfigOuterQuery query) {
        PageUtil<OperationConfigOuterResponse> pageInfo = new PageUtil<>(query);
        pageInfo.setCondition(Collections.singletonMap("query", query));
        operationConfigService.findPage(pageInfo, OperationConfigOuterResponse.class);
        return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), pageInfo);
    }

    @Override
    public ReturnResult<OperationConfigExpandResponse> find(String language, String customId) {
        OperationConfigExpand operationConfigExpand = operationConfigService.findByCustomIdAndLanguage(customId, language);
        // 没查到默认en再查一次
        if(Objects.isNull(operationConfigExpand) && StringUtils.isNoneBlank(language) && !"en".equals(language)){
            operationConfigExpand = operationConfigService.findByCustomIdAndLanguage(customId, "en");
        }

        if(Objects.isNull(operationConfigExpand)){
            return ReturnResult.success(getInternational(language, InternationalCode.NOT_FOUND));
        } else {
            OperationConfigExpandResponse response = CopyUtils.copyObject(operationConfigExpand, OperationConfigExpandResponse.class);
            return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
        }

    }

    @Override
    public ReturnResult<OperationConfigExpandResponse> findIgnoreLan(String language, String customId) {
        OperationConfigExpand operationConfigExpand = operationConfigService.findByCustomIdIgnoreLanguage(customId, language);
        // 没查到默认en再查一次
        if(Objects.isNull(operationConfigExpand) && StringUtils.isNoneBlank(language) && !"en".equals(language)){
            operationConfigExpand = operationConfigService.findByCustomIdIgnoreLanguage(customId, "en");
        }

        if(Objects.isNull(operationConfigExpand)){
            return ReturnResult.success(getInternational(language, InternationalCode.NOT_FOUND));
        } else {
            OperationConfigExpandResponse response = CopyUtils.copyObject(operationConfigExpand, OperationConfigExpandResponse.class);
            return ReturnResult.success(getInternational(language, InternationalCode.QUERY_SUCCESS), response);
        }
    }

}
